import request from '@/utils/request'
import Qs from 'qs'

export function login(data) {
  return request({
    url: 'user/login',
    method: 'post',
    headers:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},
    transformRequest: [
      function(data) {
        var ret = ''
        for (var it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        ret = ret.substring(0, ret.lastIndexOf('&'))
        return ret
      }],
    data: data
  })
}

export function getInfo() {
  return request({
    url: 'user/info',
    method: 'POST',
  })
}

export function logout() {
  return request({
    url: 'user/logout',
    method: 'post',
  })
}

export function doPut(url,data) {
  return request({
    url: url,
    method: 'put',
    data
  })
}

export function doGet(url,query) {
  return request({
    url: url,
    method: 'get',
    params: query,
    paramsSerializer: function(params) {
      return Qs.stringify(params, { arrayFormat: 'repeat' })
    }
  })
}

export function jsonPost(url,data) {
  return request({
    url: url,
    method: 'post',
    headers:{'Content-Type':'application/json'},
    data
  })
}

export function doPost(url,data) {
  return request({
    url: url,
    method: 'post',
    headers:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},
    transformRequest: [
      function(data) {
        var ret = ''
        for (var it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        ret = ret.substring(0, ret.lastIndexOf('&'))
        return ret
      }],
    data
  })
}

export function doFile(url,data) {
  return request({
    url: url,
    method: 'post',
    headers:{'Content-Type':'multipart/form-data'},
    data
  })
}

export function doDownload(url,data) {
  return request({
    url: url,
    method: 'get',
    responseType: 'blob',
    headers:{'Content-Type':'application/json'},
    params: data
  })
}

export function doForm(url,data) {
  return request({
    url: url,
    method: 'post',
    headers:{'Content-Type':'application/x-www-form-urlencoded'},
    params:data,
  })
}


// 查询部门列表（排除节点）
export function orglistChild(data) {
  return request({
    url: 'organize/treeList',
    method: 'post',
    headers:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},
    transformRequest: [
      function(data) {
        var ret = ''
        for (var it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        ret = ret.substring(0, ret.lastIndexOf('&'))
        return ret
      }],
    data
  })
}

/**
 * 获取出入点配置详情*/
export function posDetail(id) {
  return request({
    url: 'entrypos/detail/'+id,
    method: 'post',
    headers:{'Content-Type':'multipart/form-data'}
  })
}

/**
 * 获取部门数据*/
export function getOrgApi() {
  return request({
    url: 'user/getOrg',
    method: 'post',
    headers:{'Content-Type':'multipart/form-data'}
  })
}

/**
 * 获取部门负责人*/
export function getPersonneApi(data) {
  return request({
    url: 'personnellib/list',
    method: 'post',
    headers:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},
    transformRequest: [
      function(data) {
        var ret = ''
        for (var it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        ret = ret.substring(0, ret.lastIndexOf('&'))
        return ret
      }],
    data
  })
}
